
def getInFile():
    global inF
    fileLoc = "D:\\09_Projects\AlgoEcp\IO\A-large-practice.in"
    inF = open(fileLoc)

def processIn():
    global noOfTC
    noOfTC = int(inF.readline())

def processTS():
    testIn = inF.readline()
    testSpt = testIn.split()
    global noOfTS, OBTab, DistTab
    noOfTS = int(testSpt[0])
    OBTab = []
    DistTab = []
    for TS in range(0, noOfTS):
        if testSpt[TS*2 + 1] == 'O':
            OBTab.append(0)
        else:
            OBTab.append(1)
        DistTab.append(int(testSpt[TS*2 + 2]))
    #print(OBTab)
    #print(DistTab)
    #print(testIn)

def calcTime():
    B = 1
    O = 0
    bot = ['O','B']
    totEst = [0,0]
    cur = [1,1]
    nxt = [1,1]
##    print("Action:", "  Dist", " MoveT:",
##              " WaitT:",
##              " curB:", "   curO",
##              " NxtB:", "   NxtO"
##              " tEstB:", "tEstO",
##              sep='\t')
    for action in range(0, noOfTS):
        if OBTab[action] == B:
            nxt[B] = findNxtB(action)
        else:
            nxt[O] = findNxtO(action)
        move = abs(nxt[OBTab[action]] - cur[OBTab[action]])
        wait = totEst[OBTab[action]^1] - totEst[OBTab[action]] - move
        if wait < 0:
            wait = 0
        totEst[OBTab[action]] = totEst[OBTab[action]] + move + 1 + wait
##        print(bot[OBTab[action]], DistTab[action],
##              move,
##              wait,
##              cur[B], cur[O],
##              nxt[B], nxt[O],
##              totEst[B], totEst[O], sep='\t     ')
        cur[OBTab[action]] = nxt[OBTab[action]]        
    return max(totEst)

def findNxtB(curIdx):
    for idx in range(curIdx, noOfTS):
        if OBTab[idx] == 1:
            break
    return DistTab[idx]

def findNxtO(curIdx):
    for idx in range(curIdx, noOfTS):
        if OBTab[idx] == 0:
            break
    return DistTab[idx]

def processTC(tcNo):
    processTS()
    outLine = "Case #" + str(tcNo+1) + ": " + str(calcTime()) + "\n"
    print(outLine)
    outFile.write(outLine)

def initOut():
    global outFile
    outFile = open("D:\\09_Projects\AlgoEcp\IO\A-large-output.out", "w")

getInFile()
processIn()
initOut()
for tcNo in range(0, noOfTC):
    processTC(tcNo)
outFile.close()

